import java.util.ArrayList;
import java.util.List;

/**
 * @author tmh
 * @date 2024/9/28 16:36
 * @description
 */
public class T131分割回文串 {
    private List<List<String>> result = new ArrayList<>();
    private List<String> path = new ArrayList<>();

    public List<List<String>> partition(String s) {
        dfs(0, s);
        return result;
    }

    private void dfs(int beginIndex, String s) {
        if (beginIndex >= s.length()) {
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = beginIndex; i < s.length(); i++) {

            if (isValid(s, beginIndex, i)) {
                String letter = s.substring(beginIndex, i + 1);
                path.add(letter);
            } else {
                continue;
            }
            dfs(i + 1, s);
            path.remove(path.size()-1);
        }
    }

    private boolean isValid(String str, int start, int end) {
        for (int i = start, j = end ; i < j; i++, j--) {
            if (str.charAt(i) != str.charAt(j)) {
                return false;
            }
        }
        return true;
    }
}
